package org.tsinghua.answer.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.tsinghua.answer.domain.subject.Subject;
import org.tsinghua.answer.domain.subject.SubjectRepository;
import org.tsinghua.answer.domain.user.User;
import org.tsinghua.answer.domain.user.UserRepository;
import org.tsinghua.answer.domain.user.UserService;
import org.tsinghua.answer.domain.user.UserType;
import org.tsinghua.answer.domain.user.exception.EmailException;
import org.tsinghua.answer.domain.user.exception.PasswordException;
import org.tsinghua.answer.domain.user.exception.UsernameException;
import org.tsinghua.answer.utils.AuthUtils;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private SubjectRepository subjectRepository;
	
	@GetMapping("login")
	public String login() {
		if(AuthUtils.isAuthenticated()) {
			return "redirect:/";
		}
		return "login";
	}
	
	@GetMapping("regist")
	public ModelAndView regist() {
		if(AuthUtils.isAuthenticated()) {
			return new ModelAndView("redirect:/");
		}
		ModelAndView view = new ModelAndView("regist");
		List<Subject> subjects = subjectRepository.findAll();
		view.addObject("subjects", subjects);
		return view;
	}
	
	@RequestMapping(value="regist", method = RequestMethod.POST)
	public ModelAndView regist(User user) {
		ModelAndView view;
		try {
			userService.registUser(user);	
			view = new ModelAndView("redirect:login");
			return view;
		} catch (UsernameException | EmailException | PasswordException e) {
			view = regist();
			view.addObject("user", user);
			view.addObject("error", e.getMessage());
			return view;
		}
	}
	
	@GetMapping("ts/profile")
	public ModelAndView tsProfile() {
		ModelAndView view = profile();
		view.setViewName("ts/profile");
		return view;
	}
	
	@PostMapping("ts/profile")
	public ModelAndView tsProfile(User user) {
		ModelAndView view = profile(user);
		view.setViewName("ts/profile");
		return view;
	}
	
	@GetMapping("profile")
	public ModelAndView profile() {
		User user = AuthUtils.getUser();
		user = userRepository.findOne(user.getId());
		ModelAndView view = new ModelAndView("profile");
		view.addObject("user", user);
		List<Subject> subjects = subjectRepository.findAll();
		view.addObject("subjects", subjects);
		if(user.getType()!=UserType.USER) {
			view.addObject("domain", user.getType().toString().toLowerCase());
		}
		return view;
	}
	
	@PostMapping("profile")
	public ModelAndView profile(User user) {
		try {
			user = userService.updateUserProfile(user);
			ModelAndView view = profile();
			view.addObject("success", "保存成功");
			return view;
		} catch (EmailException | PasswordException e) {
			ModelAndView view = profile();
			view.addObject("error", e.getMessage());
			return view;
		}
	}
}
